package DAO;

import Utils.Stock;
import Utils.StockInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
@Repository
public class StockInfoDAO
{
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //更新股票数据
    public void UpdateStockInfo(ArrayList<StockInfo> stockInfos)
    {
        String sql = "insert into StockInfo values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        ArrayList<Object[]> objectList = new ArrayList<>();
        for(StockInfo s: stockInfos)
        {
            Object[] objects = new Object[]{s.getStockCode(),s.getLatestPrice(),s.getAOIAAOD(),s.getIAD(),s.getTradingVolume(),s.getBusinessVolume(),
            s.getAmplitude(),s.getHighestPrice(),s.getLowestPrice(),s.getTodayOpen(),s.getYesterdayClose(),s.getVolumeRatio(),s.getTurnoverRatio(),s.getPERatioDymatic()
            ,s.getPBRatio(),s.getMarketCap(),s.getCirculationMarketValue(),s.getRisingSpeed(),s.getUpAndDown5M(),s.getUpAndDown60D(),s.getUpAndDownUN()};
            objectList.add(objects);
        }
        jdbcTemplate.batchUpdate(sql,objectList);
    }
    //获得所有股票信息
    public ArrayList<StockInfo> getALLStockInfo()
    {
        String sql = "Select * from StockInfo,Stock WHERE StockInfo.StockCode = Stock.StockCode";
        ArrayList<StockInfo> StockInfoList;
        StockInfoList = (ArrayList<StockInfo>) jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(StockInfo.class));
        return StockInfoList;
    }
    //清空表数据
    public void TruncateStockInfo()
    {
        String sql = "Truncate StockInfo";
        jdbcTemplate.update(sql);
    }
    //根据股票代码获取股票信息
    public StockInfo getStockInfoByCode(String StockCode)
    {
        String sql = "Select * from StockInfo Where StockCode = ? ";
        StockInfo result = null;
        result = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<StockInfo>(StockInfo.class),StockCode);
        return result;
    }
    //分组查询
    public ArrayList<StockInfo> getAllStockInfoLimit(int start,int PageSize)
    {
        String sql = "Select * from StockInfo Limit ?,?";
        Object[] args = new Object[]{start,PageSize};
        ArrayList<StockInfo> StockInfoList;
        StockInfoList = (ArrayList<StockInfo>) jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(StockInfo.class),args);
        return StockInfoList;
    }
    //获取记录个数
    public Integer getStockInfoCount()
    {
        String sql = "Select Count(*) from StockInfo";
        Integer count;
        count = jdbcTemplate.queryForObject(sql,Integer.class);
        return count;
    }
}
